<template>
	<view class="my-profile">
		<!-- 用户信息区域 -->
		<view class="user-info" @click="gotoxinxi">
			<!-- // v-if 判断是否有无头像 -->
			<image class="avatar" v-if="user.user.userpic" :src="user.user.userpic" alt="用户头像"></image>
			<image class="avatar" v-else src="https://upload-0219.oss-cn-beijing.aliyuncs.com/996dc38931f4-th2.png"
				alt="用户头像"></image>

			<view>
				<text class="username">{{user.user.name}}</text>
				<image src="../../static/images/phone.png" style="width: 20rpx; height: 20rpx;"></image>
				<text class="phone">{{user.user.phone}}</text>
			</view>
		</view>

		<!-- 等级信息区域 -->
		<view class="level-info">
			<image src="../../static/images/zuanshi.png" style="width: 30rpx; height: 30rpx; margin-left: 20rpx;">
			</image>
			<view class="level" style="margin-left: 20rpx;">钻石级</view>
			<text class="score" style="margin-left: 330rpx;">等级分18892.00</text>
		</view>

		<!-- 功能快捷入口 -->
		<view class="shortcut-menu">
			<uni-grid :column="3" :show-border="false" :square="false" @change="change">
				<uni-grid-item v-for="(item ,index) in list" :index="index" :key="index">
					<view class="grid-item-box">
						<view>
							<image class="image" :src="item.url" mode="aspectFill" />
						</view>
						<view>
							<text class="text">{{item.text}}</text>
						</view>
						<view v-if="item.badge" class="grid-dot">
							<uni-badge :text="item.badge" :type="item.type" />
						</view>
					</view>
				</uni-grid-item>
			</uni-grid>

		</view>

		<!-- 企业认证和其他选项 -->
		<view class="other-options">
			<uni-list>
				<uni-list-item style="height: 40px;"></uni-list-item>
				<uni-list-item title="企业认证" thumb="/static/images/qiyerenzehng.png" :to="authStatus === 'done' ? '' : './pages/my/pageinfo/ruzhu'" 
					@click="handleAuthClick" thumb-size="sm" :rightText="authText" showArrow>
					<template #rightText>
						<!-- 根据认证状态动态显示右侧文本 -->
						<text v-if="authStatus === 'needReal'">未实名</text>
						<text v-else-if="authStatus === 'needCompany'">未认证</text>
						<text v-else>已认证</text>
					</template>
				</uni-list-item>
				<uni-list-item title="邀请好友" thumb="/static/images/yaoqinghaoyou.png" thumb-size="sm"
					showArrow></uni-list-item>
				<uni-list-item title="修改密码" @click="updatemima" thumb="/static/images/shezhi.png" :to="`./pages/my/pageinfo/updatemima`"  thumb-size="sm" showArrow></uni-list-item>
				<uni-list-item title="常见问题" thumb="/static/images/question.png" thumb-size="sm"
					showArrow></uni-list-item>
				<uni-list-item title="客服电话" thumb="/static/images/kefu.png" thumb-size="sm" rightText="工作时间 10:00-19:00"
					showArrow></uni-list-item>
			</uni-list>
		</view>
	</view>
</template>

<script setup>
	import {
		ref,
		computed
	} from 'vue';
	import {
		useuser
	} from '@/stores/user.js';
	import {
		onLoad
	} from '@dcloudio/uni-app'
	import {
		http
	} from '@/config/request.js'
	import "@/config/request.js";
	const user = useuser()
	console.log(user.user.name)
	// 响应式变量，用于存储用户认证状态
	const isAuthenticated = ref(0); // 实名认证
	const isAuthenticatedCompany = ref(''); // 营业执照认证
	const isAccountOpened = ref(''); // 开户行认证
	//法人认证
	const isLegalPersonAuthenticated = ref('');
	// 添加计算属性，判断当前认证阶段
	const authStatus = computed(() => {
		if (isAuthenticated.value !== 1) return 'needReal'; // 需要实名认证
		if (isAuthenticatedCompany.value !== '1' || isLegalPersonAuthenticated.value !== '1' || isAccountOpened
			.value !== '1') return 'needCompany'; // 需要企业类认证
		return 'done'; // 已完成所有认证
	});
	const authText = computed(() => {
		if (authStatus.value === 'needReal') return '实名认证中';
		if (authStatus.value === 'needCompany') return '企业认证中';
		return '已认证';
	});
	const fetchAuthenticationStatuses = async () => {
		// 营业执照认证
		const res2 = await http({
			url: '/licenseAuthentication/select/' + user.user.id,
			method: 'GET'
		});
		isAuthenticatedCompany.value = res2.shuju ? res2.shuju.auditStatus : '0';

		// 法人认证
		const res3 = await http({
			url: '/legalAuthentication/select/' + user.user.id,
			method: 'GET'
		});
		isLegalPersonAuthenticated.value = res3.shuju ? res3.shuju.auditStatus : '0';

		// 开户行认证
		const res4 = await http({
			url: '/accountAuthentication/select/' + user.user.id,
			method: 'GET'
		});
		isAccountOpened.value = res4.shuju ? res4.shuju.auditStatus : '0';
		const res5 = await http({
			url: '/driverRealname/query/' + user.user.id,
			method: 'GET',
		});
		isAuthenticated.value = res5.shuju ? res5.shuju.status : '0';
	}
	const list = ref([{
			url: '/static/images/myzhanghu.png',
			text: '我的账户'
		},
		{
			url: '/static/images/myfapiao.png',
			text: '我的发票'
		},
		{
			url: '/static/images/jiesuan.png',
			text: '结算凭证'
		},
		{
			url: '/static/images/adress.png',
			text: '地址薄'
		},
		{
			url: '/static/images/hetong.png',
			text: '合同管理'
		},
		{
			url: '/static/images/huo.png',
			text: '货源大厅'
		}
	])

	// 跳转到实名认证页面
	const gotoxinxi = () => {
		uni.navigateTo({
			url: '/pages/my/pageinfo/xinxi'
		});
	};
	const handleAuthClick = () => {
	  if (authStatus.value === 'needReal') {
	    // 跳转到实名认证页面
	    uni.navigateTo({
	      url: '/pages/homepage/pageinfo/shimingrenzheng'
	    });
	  } else if (authStatus.value === 'needCompany') {
	    // 跳转到企业认证页面
	    uni.navigateTo({
	      url: '/pages/my/pageinfo/ruzhu'
	    });
	  }el
	};
	// 跳转到修改密码页面
	const updatemima = () => {
		uni.navigateTo({
			url: '/pages/my/pageinfo/updatemima',
			});
}
	const change = (e) => {
		const {
			index
		} = e.detail
		if (list.value[index].badge) {
			list.value[index].badge++
		}
		// 根据index进行页面跳转
		switch (index) {
			case 0:
				//提示暂未开发此功能
				uni.navigateTo({
					url: '/pages/sourceofgoods/pageinfo/qianbaoa'
				});
				break;
			case 1:
				uni.showToast({
					title: '暂未开发此功能',
					icon: 'none'
				});
				break;
			case 2:
				uni.showToast({
					title: '暂未开发此功能',
					icon: 'none'
				});
				break;
			case 3:
				uni.navigateTo({
					url: '/pages/sourceofgoods/pageinfo/address'
				});
				break;
			case 4:
				uni.showToast({
					title: '暂未开发此功能',
					icon: 'none'
				});
				break;
			case 5:
				uni.switchTab({
					url: '/pages/sourceofgoods/index'
				});
				break;
			default:
				break;
		}
	}
	
	onLoad(() => {
		setTimeout(() => {
			isAuthenticated.value = user.user.dirverstatus;
			fetchAuthenticationStatuses();
			
		}, 100); // 延迟保证 user.user 初始化完成
	})
</script>

<style lang="scss" scoped>
	.image {
		width: 25px;
		height: 25px;
	}

	.text {
		font-size: 14px;
		margin-top: 5px;
	}

	.my-profile {
		padding: 10px;
	}

	.user-info {
		background-color: #002fa7;
		border-radius: 10px;
		color: white;
		padding: 15px;
		display: flex;
		align-items: center;
		margin-top: 10px;
	}

	.user-info .avatar {
		width: 40px;
		height: 40px;
		border-radius: 50%;
		margin-right: 10px;
	}

	.user-info .username {
		font-size: 16px;
		margin-right: 10px;
		display: flex;
		justify-content: space-between;
	}

	.user-info .phone {
		font-size: 14px;
	}

	.level-info {
		height: 30rpx;
		background-color: #ffdc2e;
		border-radius: 10px;
		color: white;
		padding: 10px;
		display: flex;
		/* justify-content: space-between; */
		align-items: center;
		margin-top: 10px;
	}

	.level-info .level {
		font-size: 26rpx;
	}

	.level-info .score {
		font-size: 26rpx;
	}

	.shortcut-menu {
		height: 20vh;
		margin-top: 20px;
		background-color: #ffffff;
		border-radius: 10px;
		padding: 12px;
		border: 1px solid #eeeeee;
		box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
	}

	.shortcut-menu uni-grid-item text {
		margin-top: 5px;
		font-size: 14px;
		margin-left: 40rpx;
	}

	.shortcut-menu uni-grid-item image {
		width: 50rpx;
		height: 50rpx;
		margin-top: 10rpx;
		margin-bottom: 10rpx;
		margin-left: 70rpx;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.other-options {
		margin-top: 20rpx;
		background-color: #ffffff;
		border-radius: 10px;
		/* padding: 20rpx; */
		border: 1px solid #eeeeee;
		box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
		/* display: flex; */
	}
</style>